﻿/*
 * @autor: hectorea
 * @date: 11/08/2012
 * @programming interview 3
 * 
 */
using System;

namespace StringPermutation
{
    internal static class StringExtension
    {
        internal static void Permute(this string input)
        {
            Permutation(input.ToCharArray(), 0);
        }

        private static void Permutation(char[] input, int current)
        {
            if (current == input.Length-1)
            {
                Console.WriteLine(input);
                return;
            }           

            for (int i = current; i < input.Length; i++)
            {
                input = Swap(input, current, i);
                Permutation(input, current + 1);
                input = Swap(input, current,i);
            }
        }

        private static char[] Swap(char[] input, int a, int b)
        {
            if (a != b)
            {
                input[a] ^= input[b];
                input[b] ^= input[a];
                input[a] ^= input[b];
            }

            return input;
        }
    }
}
